
const Router = require("koa-router");
const router =  new Router();
const db = require("../db");
router.get("/api/cation",async (ctx,next)=>{
	let data = await db.select(`select * from goods`);
    let newarr = [],json = {},dataArr = [];

    
    for(let i=0;i<data.length;i++){
        if(!json[data[i].type]){
        	newarr.push({
        		type:data[i].type,
        		title:data[i].title
        	})
        	json[data[i].type] = true;
        }     
    }



    for(var i=0;i<newarr.length;i++){
         toData(newarr[i].type,i)
    }

     
    ctx.body = {
        code:0,
        data:dataArr
    }

    function toData(type,index){
        let list = [];
        data.forEach(item=>{
            if(item.type==type){
                list.push({
                    imgurl:item.imgurl,
                    name:item.name,
                    id:item.id
                })
            }
        })
        dataArr.push({
             type:newarr[index].type,
             title:newarr[index].title,
             list:list
        })
    }
     
    
    


})





module.exports = router;